from flask import jsonify, Blueprint, redirect
from application.admin.model.menu_model import MenuAdmin

api_menu = Blueprint('api_menu', __name__)
api_menu.url_prefix = "/api/menu"


@api_menu.route('/index')
def menu_index():
    json='{"key":"value"}'
    return json

@api_menu.route('/admin')
def menu_admin():
    return  getSystemInit()
@api_menu.route('/test')
def menu_test():
    return  getSystemInit()
def getSystemInit():
        homeInfo = {
            'title': '首页',
            'href': '/admin/welcome',
        }
        logoInfo = {
            'title': 'NCAS-M',
            'image': "images/logo.png",
            "href": "/admin"
        }
        menuInfos= {
                "title": "NCAS-系统",
                "icon": "fa fa-address-book",
                "href": "",
                "target": "_self",
                "child": getMenuList()
        }
        menuInfo=[]#再构建一级子树
        menuInfo.append(menuInfos)
        systemInit = {
            'homeInfo': homeInfo,
            'logoInfo': logoInfo,
            'menuInfo': menuInfo,
        }
        return jsonify(systemInit)
# 获取菜单列表
def getMenuList():
    menuList = MenuAdmin.query.filter_by().all()
    menuList = buildMenuChild(0, menuList)
    return menuList
    #递归获取子菜单
def buildMenuChild(pid, menuList):
    treeList = []
    for v in menuList:
        if(pid==v.pid):
            node = {}
            node["title"] = v.title
            node["href"] = v.href
            node["icon"] = v.icon
            node["target"] = v.target
            child = buildMenuChild(v.id, menuList)
            if (child!=[]):
                node['child'] = child
            treeList.append(node)
    return treeList
